package com.apes.scm.masterdata.party.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.res.model.Currency;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.model.Country;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：Party扩展--法人公司信息
 *
 * @author HuJianLin
 * @create 2018-0-24 11:23
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Corporate.CONST_TABLE_NAME)
public class Corporate extends BaseModel {
    public static final String CONST_TABLE_NAME = "mas_corporate";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "法人公司信息")
    @Comment("法人公司编码")
    private long id;   //法人公司编码

    @OneToOne(optional = false)
    @JoinColumn(name = "PARTY_ID", referencedColumnName = "ID")
    @Comment("当事人编码")
    private Party party; //当事人

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_nature_id", referencedColumnName = "id")
    @Comment("公司性质编码")
    private Enumeration companyNature; //公司性质编码

    //注册地址（户籍地址）
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reg_country_id", referencedColumnName = "id")
    @Comment("注册地址.国家")
    private Country regCountry; //注册地址.国家

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reg_province_id", referencedColumnName = "id")
    @Comment("注册地址.省份")
    private Area regProvince; //注册地址.省份

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reg_city_id", referencedColumnName = "id")
    @Comment("注册地址.城市")
    private Area regCity; //注册地址.城市

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reg_county_id", referencedColumnName = "id")
    @Comment("注册地址.区县")
    private Area regCounty; //注册地址.区县

    @Column(name = "reg_address", length = 500)
    @Comment("注册地址.地址")
    private String regAddress; //注册地址.地址

    //办公地址
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_country_id", referencedColumnName = "id")
    @Comment("办公地点.国家")
    private Country officeCountry; //办公地点.国家

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_province_id", referencedColumnName = "id")
    @Comment("办公地点.省份")
    private Area officeProvince; //办公地点.省份

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_city_id", referencedColumnName = "id")
    @Comment("办公地点.城市")
    private Area officeCity; //办公地点.城市

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_county_id", referencedColumnName = "id")
    @Comment("办公地点.区县")
    private Area officeCounty; //办公地点.区县

    @Column(name = "office_address", length = 500)
    @Comment("办公地点.地址")
    private String officeAddress; //办公地点.地址

    @Column(name = "postal_code", length = 32)
    @Comment("邮政编码")
    private String postalCode; //邮政编码

    @Column(name = "uscc", length = 64)
    @Comment("统一社会信用代码")
    private String uscc; //统一社会信用代码（Unified social credit code）（营业执照号，税号，组织机构代码）

    @Column(name = "registered_capital", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("注册资金")
    private double registeredCapital;  //注册资金【万元】

    /*-----------------------------------------------------
     * 公司信息
     *----------------------------------------------------*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "currency_id", referencedColumnName = "id")
    @Comment("币种编码")
    private Currency currency; //币种

    @Column(name = "max_invoice_value", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("最大发票金额")
    private double maxInvoiceValue = 0.0;  //最大发票金额

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("销项税率编码")
    private Tax tax; //销项税率

    @Column(name = "fin", columnDefinition = "number(1,0) default 0")
    @Comment("是否启用财务系统")
    private boolean fin = false;

    /*-----------------------------------------------------
     * 供应商信息
     *----------------------------------------------------*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_level", referencedColumnName = "id")
    @Comment("供应商级别编码")
    private Enumeration supplierLevel; //供应商级别(supplierLevel)(0:A级;1:B级;2:C级;3:D级;4:E级)
//    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
//    private boolean valid = true; //急件供应商标记
//    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
//    private boolean valid = true; //售后供应商标记


    @Column(name = "insurance_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("保险供应商标记")
    private boolean insuranceProvider = false; //保险供应商标记

    @Column(name = "product_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("商品供应商标记")
    private boolean productProvider = false; //商品供应商标记

    @Column(name = "service_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("服务供应商标记")
    private boolean serviceProvider = false; //服务供应商标记

    @Column(name = "pay_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("支付供应商标记")
    private boolean payProvider = false; //支付供应商标记

    @Column(name = "logistics_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("物流供应商标记")
    private boolean logisticsProvider = false; //物流供应商标记

    @Column(name = "storage_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("仓储供应商标记")
    private boolean storageProvider = false; //仓储供应商标记

    @Column(name = "urgent_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("急件供应商标记")
    private boolean urgentProvider = false; //急件供应商标记

    @Column(name = "after_sale_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("售后供应商标记")
    private boolean afterSaleProvider = false; //售后供应商标记

    @Column(name = "inner_provider", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("内部供应商标记")
    private boolean innerProvider = false; //内部供应商标记

    @Column(name = "shipment_priority", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("出货优先级标记")
    private boolean shipmentPriority = false; //出货优先级标记

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_customer_id", referencedColumnName = "id")
    @Comment("对应客户")
    private PartyRole belongCustomer;

    /*-----------------------------------------------------
     * 客户信息
     *----------------------------------------------------*/
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_group", referencedColumnName = "id")
    @Comment("客户组编码")
    private Enumeration customerGroup; //客户组

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_category", referencedColumnName = "id")
    @Comment("客户类别编码")
    private Enumeration customerCategory; //客户类别

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shipment_conditions", referencedColumnName = "id")
    @Comment("装运条件编码")
    private Enumeration shipmentConditions; //装运条件

    @Column(name = "delivery_place", length = 500)
    @Comment("交货地点")
    private String deliveryPlace; //交货地点

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_credit_group", referencedColumnName = "id")
    @Comment("客户信用组编码")
    private Enumeration customerCreditGroup; //客户信用组编码(customerCreditGroup)

    @Column(name = "is_want_invoice", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("是否要发票")
    private boolean wantInvoice = false; //是否要发票

    @Column(name = "is_can_invoice", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("能否开发票")
    private boolean canInvoice = false;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效")
    private boolean valid = true; //有效

    /**
     * 直营:001072;合伙人:001071;赋能店:001073;直营子公司:001661;
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "operation", referencedColumnName = "id")
    @Comment("运营模式编码")
    private Enumeration operation; //运营模式

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "purchase_invoice", referencedColumnName = "id")
    @Comment("默认采购发票设置")
    private Enumeration purchaseInvoice; //默认采购发票设置(purchaseInvoice)

    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "LOGO",columnDefinition = "CLOB")
    @Comment("LOGO")
    private String logo;

    @Transient
    public boolean inner() {
        return this.getParty().isRole("Company");
    }

    @Column(name = "mobile_number", length = 32)
    @Comment("手机号")
    private String mobileNumber; //手机号

    @Column(name = "contact_person", length = 32)
    @Comment("联系人")
    private String contactPerson;

    @Column(name = "contact_number", length = 32)
    @Comment("联系电话")
    private String contactNumber; //手机号


    @File
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "icon",columnDefinition = "CLOB")
    @Comment("图标")
    private String icon;

    @Column(name = "paying_unit_tag", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("代付单位标记")
    private boolean payingUnitTag = false;

    @Column(name = "unit_tag", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("单位标记")
    private boolean unitTag = false;

    @Column(name = "bloc_inner_tag", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("集团内标记")
    private boolean blocInnerTag = false;

    @Column(name = "portion_delivery", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("允许部分交货")
    private boolean portionDelivery = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_company_id", referencedColumnName = "id")
    @Comment("对应公司")
    private PartyRole belongCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_supplier_id", referencedColumnName = "id")
    @Comment("对应供应商")
    private PartyRole belongSupplier;

    @Comment("负责部门(行政部门)")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "responsible_branch_id", referencedColumnName = "id")
    private Branch responsibleBranch;

    @Column(name = "legal_person", length = 32)
    @Comment("法人代表")
    private String legalPerson;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_company", referencedColumnName = "id")
    @Comment("创建公司")
    private PartyRole createCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "taxpayer_id", referencedColumnName = "id")
    @Comment("纳税人")
    private Enumeration taxpayer;
}
